package xuhaijia.lanou3g.autohome.recommend.video;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

import java.io.IOException;
import java.net.URI;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.base.BaseActivity;

/**
 * Created by 7 on 16/8/26.
 */
public class MediaPlayerActivity extends BaseActivity implements View.OnClickListener, SurfaceHolder.Callback, View.OnTouchListener {

    private VideoBean.ResultBean.ListBean bean;

    private SurfaceView surfaceView;
    private Button start;
    private MediaPlayer mediaPlayer;
    private SurfaceHolder holder;
    private Uri uri;

    @Override
    protected int setLayout() {
        return R.layout.activity_mediaplayer;
    }

    @Override
    protected void initView() {
        surfaceView = bindView(R.id.sf_view);
        surfaceView.setOnTouchListener(this);
        start = bindView(R.id.start);
        start.setOnClickListener(this);
        holder = surfaceView.getHolder();
        holder.addCallback(this);
        holder.setFixedSize(300 , 200);
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mediaPlayer.start();
            }
        });
    }

    @Override
    protected void initData() {
        bean = new VideoBean.ResultBean.ListBean();
        Intent intent = getIntent();
        bean = (VideoBean.ResultBean.ListBean) intent.getSerializableExtra("bean");
        String url = bean.getVideoaddress();
        uri = Uri.parse(url);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:


                break;
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mediaPlayer.reset();
        try {
            mediaPlayer.setDisplay(holder);
            mediaPlayer.setDataSource(getApplicationContext() , uri);
            mediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (mediaPlayer!=null) {
            mediaPlayer.stop();
            //释放资源
            mediaPlayer.release();
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                } else {
                    mediaPlayer.start();
                }
                break;
        }
        //返回True代表事件已经处理了
        return true;
    }
}
